<?php
class Modeledithomepagesupportonline extends Model {
	public function addsupport_online($data) {
		$sql = "INSERT INTO " . DB_PREFIX . "support_online SET ";
	
		foreach ($data['support_description'] as $language_id => $value) {
			$sql.=" full_name = '" . $this->db->escape($value['title']) . "', description = '" . $this->db->escape($value['description']) . "',";
		}
	
		$sql.=" user_name = '" . $this->db->escape($data['user_name']) . "', position = '".$this->db->escape($data['position'])."', phone = '".$this->db->escape($data['phone'])."'";
		$this->db->query($sql);
	}
	
	public function editsupport_online($support_id, $data) {
		$sql = "UPDATE " . DB_PREFIX . "support_online SET ";
	
		foreach ($data['support_description'] as $language_id => $value) {
			$sql.=" full_name = '" . $this->db->escape($value['title']) . "', description = '" . $this->db->escape($value['description']) . "',";
		}
	
		$sql.=" user_name = '" . $this->db->escape($data['user_name']) . "', position = '".$this->db->escape($data['position'])."', phone = '".$this->db->escape($data['phone'])."' where support_id=".$support_id;
		
		
		$this->db->query($sql);
	}
	
	public function deletesupport_online($support_id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "support_online WHERE support_id = '" . (int)$support_id . "'");
	}	

	public function getsupport_online($support_id) {
		$query = $this->db->query("SELECT  * from ". DB_PREFIX ."support_online where support_id=".$support_id);
		
		return $query->row;
	}
		
	public function getsupport_onlines($data = array()) {
		if ($data) {
			$sql = "SELECT * FROM " . DB_PREFIX . "support_online";
		
			$sort_data = array(
				'id.title',
				'i.sort_order'
			);		
		
			if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
				$sql .= " ORDER BY " . $data['sort'];	
			} else {
				$sql .= " ORDER BY full_name";	
			}
			
			if (isset($data['order']) && ($data['order'] == 'DESC')) {
				$sql .= " DESC";
			} else {
				$sql .= " ASC";
			}
		
			if (isset($data['start']) || isset($data['limit'])) {
				if ($data['start'] < 0) {
					$data['start'] = 0;
				}		

				if ($data['limit'] < 1) {
					$data['limit'] = 20;
				}	
			
				$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
			}	
			
			$query = $this->db->query($sql);
			
			return $query->rows;
		} else {
			$information_data = $this->cache->get('information.' . $this->config->get('config_language_id'));
		
			if (!$information_data) {
				$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "information i LEFT JOIN " . DB_PREFIX . "information_description id ON (i.information_id = id.information_id) WHERE id.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY id.title");
	
				$information_data = $query->rows;
			
				$this->cache->set('information.' . $this->config->get('config_language_id'), $information_data);
			}	
	
			return $information_data;			
		}
	}
	
	public function getsupport_onlineDescriptions($support_id) {
		$information_description_data = array();
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "support_online WHERE support_id = '" . (int)$support_id . "'");

		foreach ($query->rows as $result) {
			$information_description_data[1] = array(
				'title'       => $result['full_name'],
				'description' => $result['description']
			);
		}
		
		return $information_description_data;
	}
	
	public function getInformationStores($information_id) {
		$information_store_data = array();
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "information_to_store WHERE information_id = '" . (int)$information_id . "'");

		foreach ($query->rows as $result) {
			$information_store_data[] = $result['store_id'];
		}
		
		return $information_store_data;
	}
	
	public function getTotalsupport_onlines() {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "support_online");
		
		return $query->row['total'];
	}	
}
?>